#include <stdio.h>
#include <stdlib.h>

#include <evnetplus/event_loop_thread.h>

#include <evnetplus/httpc/conn_pool.h>
#include <evnetplus/httpc/request.h>
#include <evnetplus/httpc/conn.h>
#include <evnetplus/httpc/response.h>

static int responsed = 0;
static int retried = 0;
static void HandleHTTPResponse(const std::shared_ptr<evnetplus::httpc::Response>& r, evnetplus::httpc::Request* req) {
    std::cout << "http_code=" << r->http_code() << " [" << r->body().ToString() << "]" << std::endl;
    responsed++;
    if (retried < 3) {
        retried++;
        req->Execute(std::bind(&HandleHTTPResponse, std::placeholders::_1, req));
    } else {
        delete req;
    }
}

int main() {
    evnetplus::EventLoopThread t;
    t.Start(true);
    evnetplus::httpc::Request* r = new evnetplus::httpc::Request(t.loop(), "http://113.45.149.178/all/200/1//example_full_ota_1001_LuatOS_CSDK_EC618.sota", "", evnetplus::Duration(2.0));
    std::cout << "Do http request" << std::endl;
    r->Execute(std::bind(&HandleHTTPResponse, std::placeholders::_1, r));

    while (responsed != 4) {
        usleep(1);
    }

    t.Stop(true);
    std::cout << "EventLoopThread stopped." << std::endl;
    return 0;
}
